package bb.lanxing.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class IdcardUtils {
    public static final int[] power = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};

    public static Map<String, String> cityCodes = new HashMap();
    public static Map<String, Integer> twFirstCode = new HashMap();
    public static Map<String, Integer> hkFirstCode = new HashMap();

    static {
        cityCodes.put("11", "北京");
        cityCodes.put("12", "天津");
        cityCodes.put("13", "河北");
        cityCodes.put("14", "山西");
        cityCodes.put("15", "内蒙古");
        cityCodes.put("21", "辽宁");
        cityCodes.put("22", "吉林");
        cityCodes.put("23", "黑龙江");
        cityCodes.put("31", "上海");
        cityCodes.put("32", "江苏");
        cityCodes.put("33", "浙江");
        cityCodes.put("34", "安徽");
        cityCodes.put("35", "福建");
        cityCodes.put("36", "江西");
        cityCodes.put("37", "山东");
        cityCodes.put("41", "河南");
        cityCodes.put("42", "湖北");
        cityCodes.put("43", "湖南");
        cityCodes.put("44", "广东");
        cityCodes.put("45", "广西");
        cityCodes.put("46", "海南");//TODO:sun
        cityCodes.put("50", "重庆");
        cityCodes.put("51", "四川");
        cityCodes.put("52", "贵州");
        cityCodes.put("53", "云南");
        cityCodes.put("54", "西藏");
        cityCodes.put("61", "陕西");
        cityCodes.put("62", "甘肃");
        cityCodes.put("63", "青海");
        cityCodes.put("64", "宁夏");
        cityCodes.put("65", "新疆");
        cityCodes.put("71", "台湾");
        cityCodes.put("81", "香港");
        cityCodes.put("82", "澳门");
        cityCodes.put("91", "国外");
        twFirstCode.put("A", 10);
        twFirstCode.put("B", 11);
        twFirstCode.put("C", 12);
        twFirstCode.put("D", 13);
        twFirstCode.put("E", 14);
        twFirstCode.put("F", 15);
        twFirstCode.put("G", 16);
        twFirstCode.put("H", 17);
        twFirstCode.put("J", 18);
        twFirstCode.put("K", 19);
        twFirstCode.put("L", 20);
        twFirstCode.put("M", 21);
        twFirstCode.put("N", 22);
        twFirstCode.put("P", 23);
        twFirstCode.put("Q", 24);
        twFirstCode.put("R", 25);
        twFirstCode.put("S", 26);
        twFirstCode.put("T", 27);
        twFirstCode.put("U", 28);
        twFirstCode.put("V", 29);
        twFirstCode.put("X", 30);
        twFirstCode.put("Y", 31);
        twFirstCode.put("W", 32);
        twFirstCode.put("Z", 33);
        twFirstCode.put("I", 34);
        twFirstCode.put("O", 35);
        hkFirstCode.put("A", 1);
        hkFirstCode.put("B", 2);
        hkFirstCode.put("C", 3);
        hkFirstCode.put("R", 18);
        hkFirstCode.put("U", 21);
        hkFirstCode.put("Z", 26);
        hkFirstCode.put("X", 24);
        hkFirstCode.put("W", 23);
        hkFirstCode.put("O", 15);
        hkFirstCode.put("N", 14);
    }

    public static String conver15CardTo18(String idCard) {
        Date date;
        if (idCard.length() == 15 && isNum(idCard)) {
            try {
                date = new SimpleDateFormat("yyMMdd").parse(idCard.substring(6, 12));
            } catch (ParseException e) {
                e.printStackTrace();
                date = null;
            }
            Calendar calendar = Calendar.getInstance();
            if (date != null) {
                calendar.setTime(date);
            }
            String str = idCard.substring(0, 6) + String.valueOf(calendar.get(1)) + idCard.substring(8);
            char[] charArray = str.toCharArray();
            if (charArray != null) {
                String checkCode18 = getCheckCode18(getPowerSum(converCharToInt(charArray)));
                if (checkCode18.length() > 0) {
                    return str + checkCode18;
                }
                return null;
            }
            return str;
        }
        return null;
    }

    public static int[] converCharToInt(char[] ca) {
        int length = ca.length;
        int[] iArr = new int[length];
        for (int i = 0; i < length; i++) {
            try {
                iArr[i] = Integer.parseInt(String.valueOf(ca[i]));
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }
        return iArr;
    }

    public static int getPowerSum(int[] iArr) {
        if (power.length == iArr.length) {
            int i = 0;
            for (int i2 = 0; i2 < iArr.length; i2++) {
                int i3 = 0;
                while (true) {
                    int[] iArr2 = power;
                    if (i3 < iArr2.length) {
                        if (i2 == i3) {
                            i += iArr[i2] * iArr2[i3];
                        }
                        i3++;
                    }
                }
            }
            return i;
        }
        return 0;
    }

    public static String getCheckCode18(int iSum) {
        switch (iSum % 11) {
            case 0:
                return "1";
            case 1:
                return "0";
            case 2:
                return "x";
            case 3:
                return "9";
            case 4:
                return "8";
            case 5:
                return "7";
            case 6:
                return "6";
            case 7:
                return "5";
            case 8:
                return "4";
            case 9:
                return "3";
            case 10:
                return "2";
            default:
                return "";
        }
    }

    public static long getBirthTimestampByIdCard(String idCard) {
        Integer valueOf = idCard.length();
        if (valueOf < 15) {
            return 0L;
        }
        if (valueOf == 15) {
            idCard = conver15CardTo18(idCard);
        }
        Date date = null;
        try {
            date = new SimpleDateFormat("yyyyMMdd").parse(idCard.substring(6, 14));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (date != null) {
            return date.getTime();
        }
        return 0L;
    }

    public static String getGenderByIdCard(String idCard) {
        if (idCard.length() == 15) {
            idCard = conver15CardTo18(idCard);
        }
        return Integer.parseInt(idCard.substring(16, 17)) % 2 != 0 ? "M" : "F";
    }

    public static boolean isNum(String val) {
        if (val == null || "".equals(val)) {
            return false;
        }
        return val.matches("^[0-9]*$");
    }
}